<script setup>
import { ref } from 'vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import ArticleEdit from './components/ArticleEdit.vue'
import { artGetListService, artDelService } from '@/api/article'
import { formatTime } from '@/utils/format'

// 加载
const loading = ref(false)
// 定义文章列表  请求参数对象
const params = ref({
  pagenum: 1, //第几页
  pagesize: 5, //每页几条数据
  cate_id: '', //文章分类id
  state: '' //文章状态
})

// 文章列表
const articleList = ref([])
// 文章总条数
const total = ref(0)
// 获取文章列表
const getArticleList = async () => {
  loading.value = true
  const res = await artGetListService(params.value)
  articleList.value = res.data.data
  total.value = res.data.total
  loading.value = false
  // console.log(articleList.value)
}
getArticleList()
// 页码变化
const onCurrentChange = (page) => {
  // console.log('页码变了', page)
  params.value.pagenum = page
  // 重新渲染
  getArticleList()
}
// 搜索按钮
const searchArtucle = () => {
  // 重置到第一页
  params.value.pagenum = 1
  getArticleList()
}
// 重置按钮
const resteId = () => {
  // 重置到第一页
  params.value.pagenum = 1
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList()
}

// 绑定抽屉
const articleEditRef = ref()
// 添加文章
const onAddArticle = () => {
  articleEditRef.value.open({})
}
// 编辑文章
const onEditArtucle = (row) => {
  articleEditRef.value.open(row)
}
// 删除文章
const onDeleteArtucle = async (row) => {
  await artDelService(row.id)
  getArticleList()
  ElMessage.success('删除成功')
}

// 添加或者编码 成功的回调
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加，渲染到第一页
    params.value.pagenum = 1
  } else {
    // 如果是编辑，直接渲染当前页
  }
  getArticleList()
}
</script>
<template>
  <pageContainer title="文章管理">
    <template #extra>
      <el-button type="primary" @click="onAddArticle"
        >添加文章</el-button
      ></template
    >
    <!-- 具名插槽 -->
    <!-- 表单区域 -->
    <el-form inline="">
      <el-form-item label="文章分类：">
        <!-- Vue3  => v-model  => :modelValue 和 @update:modelValue 的简写 -->
        <!-- 父传子 -->
        <channel-select v-model="params.cate_id" width="150px"></channel-select>
      </el-form-item>
      <el-form-item label="发布状态：">
        <el-select style="width: 150px" v-model="params.state">
          <el-option label="已发布" value="已发布"></el-option>
          <el-option label="草稿" value="草稿"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="searchArtucle">搜索</el-button>
        <el-button @click="resteId">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="articleList" v-loading="loading">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="onEditArtucle(row)"
            >{{ row.title }}</el-link
          >
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"></el-table-column>
      <el-table-column label="发布时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"></el-table-column>
      <el-table-column label="操作" prop="">
        <!-- 利用作用域插槽 row 可以获取当前行的数据  => v-for 的 item -->
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            circle
            plain
            @click="onEditArtucle(row)"
          ></el-button>
          <el-button
            type="danger"
            :icon="Delete"
            circle
            plain
            @click="onDeleteArtucle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="demo-pagination-block">
      <el-pagination
        v-model:current-page="params.pagenum"
        v-model:page-size="params.pagesize"
        :page-sizes="[1, 2, 5, 10]"
        :background="true"
        layout="jumper,total, prev, pager, next "
        :total="total"
        @current-change="onCurrentChange"
        style="margin-top: 20px; justify-content: flex-end"
      />
    </div>
    <!-- 添加和编辑文章的抽屉 -->
    <article-edit
      ref="articleEditRef"
      @success="onSuccess"
      :withd="withd"
    ></article-edit>
  </pageContainer>
</template>

<style lang="scss" scoped></style>
